package com.ruoyi.quartz.task;

import com.ruoyi.domain.WxGame;
import com.ruoyi.utils.CrawlPage;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 人事任务调度
 */
@Component("hrTask")
public class HrTask
{

  private String url="https://pvp.qq.com/match/kpl/KPL2022S1/index.shtml";

  public static void main(String[] args){
    try {
      List<WxGame> wxGames = new HrTask().getWxGames();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public List<WxGame> getWxGames() throws  Exception{
//     Document doc = getDocument(url);
    CrawlPage crawlPage = new CrawlPage();
    Document doc = crawlPage.getDocumentByHtmunit();
//     Elements select = doc.select(".on");
//      Element li = select.select("li").get(0);
//      String gameOf = li.text();
//      System.out.println("gameOf"+gameOf);
//    Element matchList = doc.getElementById("matchList");
    Elements select1 = doc.select("#matchBox");

    Elements swiperSelect = doc.select(".g-wrap");
    //.select(".schedule .ovh .pr").select(".schedule_inner").select(".match_box");
    Elements select = swiperSelect.select(".schedule .ovh .pr");
    for (Element element : swiperSelect) {
      String location = element.select(".stage-icon").text();
      String matchYear = element.select(".match-years").text();
      String matchTime = element.select(".match-time").text();
      Elements teamSelect = element.select(".match-team .team");
      Element leftTeamElement = teamSelect.get(0);
      String leftTeamLogo=leftTeamElement.select(".team-pic").get(0).getElementsByTag("img").attr("src");
      String leftTeamName = leftTeamElement.select(".team-info").get(0).text();
      Element rightTeamElement = teamSelect.get(1);
      String rightTeamLogo=rightTeamElement.select(".team-pic").get(0).getElementsByTag("img").attr("src");
      String righteamName = rightTeamElement.select(".team-info").get(0).text();
      Elements scoreSelect = element.select(".match-score");
      Elements teamSocreSelect = scoreSelect.select(".fr bgn");
      String leftTeamSocre = teamSocreSelect.get(0).text();
      String rightTeamSocre = teamSocreSelect.get(1).text();
      String score=leftTeamSocre+":"+rightTeamSocre;
      String bestOfWhat = element.select(".match-name").get(0).text();
    }
    return  null;
  }


//  public List<WxGame> getBlogs() throws  Exception{
//    Document doc = getDocument(url);
//    Elements select = doc.select(".container-header").select(".container-header-ul");
//    int count=0;
//    for (Element element : select) {
//      Element ul = element.select("ul").get(0);
//      Element li = ul.select("li").get(0);
//      String blogNum = li.text();
//      System.out.println(blogNum);
//      String objStr=blogNum.substring(blogNum.indexOf("(")+1,blogNum.indexOf(")"));
//      System.out.println(objStr);
//      int i = Integer.parseInt(objStr);
//      double num= (double)i/40;
//      System.out.println("num:"+num);
//      count= (int) Math.ceil(num);
//    }
//    System.out.println(count);
//    for (int i = 0; i < count; i++) {
//      PageData page = new PageData();
//      page.setPage(i + 1);
//      List<Blog> blogs = new ArrayList<>();
//      int num=i+1;
//      doc = getDocument(url + "/article/list/"+num);
//
//
//      Elements div = doc.select(".article-list").select(".article-item-box").select(".csdn-tracking-statistics");
//
//
//      if (div.size()!=0) {
//        for (Element element : div) {
//          Blog blog = new Blog();
//          Elements h4s = element.select("h4");
//          for (Element h4 : h4s) {
//            Elements a = h4.getElementsByTag("a");
//            String href = a.get(0).attr("href");
//            href=href.replaceAll("https","http");
//            blog.setHref(href);
//            String title = h4.text();
//            blog.setTitle(title);
//          }
//
//          Elements contents = element.select(".content");
//          if (contents.size() != 0) {
//            for (Element content : contents) {
//              String text = content.text();
//              blog.setContent(text);
//            }
//          }
//          Elements dataNums = element.select(".info-box").select(".d-flex").select(".align-content-center");
//          for (Element dataNum : dataNums) {
//            Elements createDatas = dataNum.select(".date");
//            String createData = createDatas.get(0).text();
//            Elements spans = dataNum.select(".read-num");
//            String readNum = "0";
//            if (spans.size() != 0) {
//              readNum = spans.get(0).text();
//            }
////                String remarkNum = spans.get(2).text();
//            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            System.out.println(df.parse(createData));
//            blog.setCreateTime(df.parse(createData));
//            blog.setReadNum(Integer.valueOf(readNum));
////                blog.setRemarkNum(Integer.valueOf(remarkNum==""? "0":readNum));
//          }
//          blog.setGenerateTime(new Date());
//          blogs.add(blog);
//        }
//      }
//      page.setBlogs(blogs);
//      pageData.add(page);
//    }
//    return  pageData;
//  }

  public Document getDocument(String url) throws IOException {
//    Document doc = Jsoup.connect(url)
//      .header("Accept-Encoding", "gzip, deflate, br")
//      .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36")
//      .maxBodySize(0)
//      .timeout(600000)
//      .get();

    Document doc = Jsoup.connect(url)
      .userAgent("Mozilla")
      .cookie("auth", "token")
      .timeout(3000).get();

//    Map<String, String> cookies = null;
//    Connection.Response res = Jsoup.connect(url).timeout(30000).execute();
//    cookies = res.cookies();
//    Document doc = null;
//    doc = Jsoup.connect(url).cookies(cookies).timeout(30000).get();
    return doc;
  }


  public void doWork(){
    System.out.println("hrTask执行doWork");
  }


}
